using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Ant.DbServiceInterface;
using Ant.Dto.PersonnelDto;
using Ant.EntityFrameworkCore.Model;
using Microsoft.EntityFrameworkCore;

namespace Ant.DbService
{
    /// <summary>
    /// 数据服务提供类
    /// </summary>
    public class PersonneDataService : IPersonneDataService
    {
        private AntDB2Context _dbContext;
        public PersonneDataService(AntDB2Context dbContext)
        {
            _dbContext = dbContext;
        }

        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool AddRole(string name)
        {
            _dbContext.Add(new Role() { RoleName = name });
            return _dbContext.SaveChanges() > 0;
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool AddUser(UserInfo user)
        {
            _dbContext.UserInfos.Add(user);
            return _dbContext.SaveChanges() > 0;
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public bool DelUser(int userID)
        {
            var temp = _dbContext.UserInfos.Where(u => u.UserId == userID).FirstOrDefault();
            if (temp == null)
                return false;
            _dbContext.UserInfos.Remove(temp);
            return _dbContext.SaveChanges() > 0;
        }

        public async Task<bool> ExistUser(string email, string pwd)
        {
            return await _dbContext.UserInfos.AnyAsync(u => u.UserEmail == email && u.UserPwd == pwd);

        }

        /// <summary>
        ///  用户退出商店
        /// </summary>
        /// <param name="storeID"></param>
        /// <param name="userID"></param>
        /// <returns></returns>
        public bool ExitStore(int storeID, int userID)
        {
            var user = _dbContext.UserInfos.Where(u => u.UserId == userID && u.UserStore == storeID).FirstOrDefault();
            if (user == null)
            {
                return false;
            }
            user.UserStore = null;
            return _dbContext.SaveChanges() > 0;
        }

        public async Task<IEnumerable<Role>> GetRoleAsync()
        {
            return await _dbContext.Roles.ToListAsync();
        }

        public async Task<IEnumerable<UserInfo>> GetUserAsync()
        {
            return await _dbContext.UserInfos.ToListAsync();
        }

        public async Task<UserInfo> GetUserByEmailAsync(string email)
        {
            return await _dbContext.UserInfos.Include(u => u.UserRoleNavigation)
                                 .Where(u => u.UserEmail == email)
                                 .FirstOrDefaultAsync();


        }

        public bool UpdateJoinStore(int storeID, int userID)
        {
            var store = _dbContext.StoreInfos.Where(s => s.StoreId == storeID).FirstOrDefault();
            if (store == null)
            {
                return false;
            }
            var user = _dbContext.UserInfos.Where(u => u.UserId == userID).FirstOrDefault();
            user.UserStore = storeID;
            return _dbContext.SaveChanges() > 0;
        }

        public async Task<bool> UpdateRoleAsync(RoleDto roleDto)
        {
            var roleObj = await _dbContext.Roles.Where(r => r.RoleId == roleDto.RoleId).FirstOrDefaultAsync();
            if (roleObj == null)
                return false;
            roleObj.RoleName = roleDto.RoleName;
            return await _dbContext.SaveChangesAsync() > 0;
        }

        public Task<bool> UpdateUserEmailAsync(UpdateUserNameDto updateUserEmailDto)
        {
            throw new System.NotImplementedException();
        }

        public async Task<bool> UpdateUserNameAsync(UpdateUserNameDto updateUserNameDto)
        {

            var userObj = await _dbContext.UserInfos.Where(u => u.UserId == updateUserNameDto.UserId).FirstOrDefaultAsync();
            if (userObj == null)
                return false;
            userObj.UserName = updateUserNameDto.UserName;
            return await _dbContext.SaveChangesAsync() > 0;
        }

        public async Task<bool> UpdateUserRoleAsync(UpdateUserRoleDto updateUserRoleDto)
        {
            var userObj = await _dbContext.UserInfos.Where(u => u.UserId == updateUserRoleDto.UserId).FirstOrDefaultAsync();
            if (userObj == null)
                return false;
            userObj.UserRole = updateUserRoleDto.UserRole;
            return await _dbContext.SaveChangesAsync() > 0;
        }
    }
}